<?php
class PersonasController extends AppController {

	var $name = 'Personas';
	var $components = array('RequestHandler');
	
	function index(){
		$this->layout="ajax";	
		
		if($this->JQGridPaginate->isGrid){
			$fields = array(
			'Persona.id',
			'Persona.codigo',
			'Persona.nombre',
			'Persona.ap_paterno',
			'Persona.ap_materno',
			'Persona.fech_nacimiento',
			'Persona.direccion',
			'Persona.telefono',
			'Persona.movil',
			'Persona.mail',
			'Persona.dni',
			'Persona.estado_civil',
			'Persona.sexo',
			'Persona.ocupacion',
			'Persona.alergico',
			'Persona.padre',
			'Persona.madre',
			'Persona.nivel_academico',
			'Persona.lugar_estudio',
			'Persona.tipo',
			'Persona.ruc',
			'Persona.razonsocial',
			'Persona.nombre_completo',
			'"Ubigeo"."distrito" || \', \' || "Ubigeo"."provincia" || \', \' || "Ubigeo"."departamento" AS "Persona__ubigeo"'
			);
			
			$this->JQGridPaginate->recursive = 0;
			$this->JQGridPaginate->cakefields = $fields;
			echo $this->JQGridPaginate->paginate("Persona");
			$this->autoRender = false;
		
		}elseif($this->RequestHandler->isPost()){	
			$oper = $this->params['form']['oper'];
			if($oper == "add"){
			    //$this->data['Deposito']['usuario_id'] = 1;
			    $this->data['Persona']['nombre_completo'] = 
				$this->data['Persona']['ap_paterno'] . ' ' . 
				$this->data['Persona']['ap_materno'] . ', ' .
				$this->data['Persona']['nombre'] ;
				//var_dump ($this->data['Demanda']);
				$this->Persona->create();				 
			}elseif ($oper == "edit"){
				$this->data['Persona']['id'] = $this->params['form']['id'];
			}elseif ($oper == "del"){
				$this->data['Persona']['id'] = $this->params['form']['id'];
				//$this->data['Demanda']['estado_id'] = 2;				
			}
			//Configure::write('debug',2);
			//pr($this->data);
			$this->Persona->save($this->data['Persona']);
			echo json_encode($this->Persona->visualizarError($this->Persona->validationErrors));
			$this->autoRender = false;
		}		
	}
	
	function listaasistencia()
	{
		$this->layout="ajax";
		
        $this->loadModel('Asistencia');
		/*$listaasistencia = $this->Asistencia->find('list',array('fields'=>array('Asistencia.id'),
															'conditions' => array('Asistencia.fecha =' => date('Y-m-d'),
																'Asistencia.matricula_id =' => $_POST['matri'])));		
																*/
		/*if (empty($listaasistencia))
		{
	        $this->loadModel('Matricula');
				$fields = array(
				'Matricula.id',
				'"Persona"."ap_paterno" AS "Matricula__ap_paterno"',
				'"Persona"."ap_materno" AS "Matricula__ap_materno"',
				'"Persona"."nombre" AS "Matricula__nombre"',
				'"Persona"."codigo" AS "Matricula__codigo"',
				);
				
				$this->JQGridPaginate->recursive = 0;
				$this->JQGridPaginate->cakefields = $fields;
				$this->JQGridPaginate->condition = array('Matricula.programacion_id'=>$_POST['matri']);
				$this->autoRender = false;	
				echo $this->JQGridPaginate->paginate("Matricula");
		}
		else*/
		{
				$fields = array(
				);
				
				$this->JQGridPaginate->recursive = 0;
				$this->JQGridPaginate->cakefields = $fields;
				//$this->JQGridPaginate->condition = array('Asistencia.programacion_id'=>$_POST['matri']);
				$this->autoRender = false;	
				echo $this->JQGridPaginate->paginate("Asistencia");
		}
	}
	
	
	function listadocentes(){
		$this->layout="ajax";
		$listaDocentes = $this->Persona->find('list',array('fields'=>array('Persona.id','Persona.nombre_completo'),
															'order'=>'Persona.nombre_completo',));
															//'conditions' => array('Article.status !=' => 'pending')
		echo json_encode($listaDocentes);
		$this->autoRender = false;
	}
	
	function getPersonasLista(){
		
		$personasLista = $this->getPersonaListaJson($this->params['url']['term']);
		
		echo $personasLista;
		$this->autoRender = false;
	}
	
	function getPersonaListaJson($art){

		
		$articuloLista = $this->Persona->find('all',array(
								'fields'=>array('Persona.id','"Persona"."codigo" || \' - \' || "Persona"."nombre_completo" AS "Persona__label"'
		                                        ,'"Persona"."codigo" || \' - \' || "Persona"."nombre_completo" AS "Persona__value"'),
								//'conditions'=>array('Persona.estado_id'=>1, '"Persona"."codigo" || \' - \'|| "Persona"."nombre_completo" ILIKE \'%'.trim($art).'%\''),
		                        'conditions'=>array('"Persona"."codigo" || \' - \'|| "Persona"."nombre_completo" ILIKE \'%'.trim($art).'%\''),
								));
								
		$tmp_1 = array('Persona'=>array(array('id'=>null, 'label'=>'No se encontraron coincidencias', 'value'=>trim($art))));	
		foreach($articuloLista as $key => $articulo){
			
			$tmp['Persona'][$key]['label'] = $articulo['Persona']['label'];
			$tmp['Persona'][$key]['value'] = $articulo['Persona']['value'];
			$tmp['Persona'][$key]['id'] = $articulo['Persona']['id'];
		}
		pr(($tmp)?json_encode($tmp_1['Persona']):json_encode($tmp['Persona']));
		return empty($tmp)?json_encode($tmp_1['Persona']):json_encode($tmp['Persona']);
	}
}
